package web.controller;

import com.alibaba.fastjson.JSONObject;
import common.bean.SearchPageResult;
import common.bean.SearchParameter;
import common.tag.easyui.DataGrid;
import common.tag.easyui.TagUtils;
import module.model.*;
import module.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/evaluation")
public class EvaluationController extends BaseController {
    public final static String prefix="admin/module/evaluation/";

    protected String getPrefix() {
        return prefix;
    }

    @Autowired
    private AssessmentRecordService assessmentRecordService;

    @Autowired
    private ServiceModelService serviceModelService;

    @Autowired
    private EvaluationFormService evaluationFormService;

    @Autowired
    private AssessmentDataService assessmentDataService;

    @Autowired
    private AssessmentRecordValueService assessmentRecordValueService;

    @RequestMapping(value = "recordList",method = RequestMethod.GET)
    public String dataList(ModelMap map, Long serviceId){
        map.put("serviceModel",serviceModelService.getServiceModelByServiceId(serviceId));
        return prefix+"list";
    }

    @RequestMapping(value = "recordList",method = RequestMethod.POST)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.OK)
    public void list(DataGrid dataGrid,Long serviceId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.addParam("serviceId",serviceId);
        searchParameter.setPageIdx(page);
        SearchPageResult<AssessmentRecordModel> serviceModelSearchPageResult = assessmentRecordService.getAssessmentRecordModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "evaluationFormList",method = RequestMethod.POST)
    @ResponseBody
    public List<EvaluationFormModel> getEvaluationForm(){
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPagination(false);
        return evaluationFormService.getEvaluationFormModelListBySearchParameter(searchParameter);
    }

    @RequestMapping(value = "recordAdd",method = RequestMethod.GET)
    public String recordAdd(ModelMap map, Long serviceId){
        map.put("curAdminId",getCurAdminId());
        return prefix+"recordAdd";
    }

    @RequestMapping(value = "loadForm")
    public String loadForm(ModelMap map, Long formId){
        EvaluationFormModel evaluationFormModel = evaluationFormService.getEvaluationFormModelByFormId(formId);
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPagination(false);
        searchParameter.addParam("formId",formId);
        SearchPageResult<AssessmentDataModel> serviceModelSearchPageResult = assessmentDataService.getAssessmentDataModelListPage(searchParameter);
        map.put("evaluationFormModel",evaluationFormModel);
        map.put("assessmentDataList",serviceModelSearchPageResult.getResult());
        return prefix+"loadForm";
    }

    @RequestMapping(value = "recordPost",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap recordPost(@RequestBody List<AssessmentRecordValueModel> assessmentRecordValueModels){
        ModelMap map = new ModelMap();
        try {
            assessmentRecordValueService.saveListAssessmentRecordValueModel(assessmentRecordValueModels,getCurAdminId());
            setSuccess(map);
        }catch (Exception e){
            setFailure(map);
            e.printStackTrace();
        }
        return map;
    }
}
